<%@ taglib prefix="spring-tags" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="spring-form" uri="http://www.springframework.org/tags/form"%>
<%@ page
    import="org.jiscinvolve.astrodabis.mallow.model.api.Tag"
    import="org.jiscinvolve.astrodabis.mallow.model.api.Query"
    import="org.jiscinvolve.astrodabis.mallow.model.api.QueryTag"
    import="org.jiscinvolve.astrodabis.mallow.model.api.Result"
    import="org.jiscinvolve.astrodabis.mallow.model.api.Account"
    import="org.jiscinvolve.astrodabis.mallow.model.tap.TapService"
    import="org.jiscinvolve.astrodabis.mallow.control.ViewData"
    session="true"
%>
<%@ include file="/jsp/init.jsp" %>
<%@ include file="/jsp/queries/init.jsp" %>
<html>
    <head>
	    <title>AstroDAbis query <%= query.name() %></title>
        <link href='/astrodabis/css/page.css' rel='stylesheet' type='text/css'/>
    </head>
    <body>
        <%@ include file="/jsp/header.jsp" %>
        <div class='body'>
            <div class='left'>
                <%@ include file="/jsp/menu.jsp" %>
            </div>
            <div class='right'>
                <a href='<%= data.path(query) %>'>
                    <img src='<%= data.qrcode(data.path(query)) %>'/>
                </a>
            </div>
            <div class='content'>
        	    <h2><%= query.owner().name() %>&apos;s query <%= query.name() %></h2>
                <fieldset>
                    <legend>Query</legend>
                    <fieldset>
                        <legend>Details</legend>
                        <div>
                            <ul>
                                <li>
                                    Name : <%= query.name() %>
                                </li>
                                <li>
                                    Created : <%= query.created() %>
                                </li>
                                <li>
                                    Owner : <a href='<%= data.path(query.owner()) %>'><%= query.owner().name() %></a>
                                </li>
                                <li>
                                    Service : <a href='<%= data.path(query.table().service()) %>'><%= query.table().service().name() %></a>
                                </li>
                                <li>
                                    Table : <a href='<%= data.path(query.table()) %>'><%= query.table().name() %></a>
                                </li>
                                <li>
                                    Status : <%= query.status() %>
                                </li>
                            </ul>
                        </div>
                        <%
                        if (data.owner(query))
                            {
                            %>
                            <a href='<%= data.path(query, "edit") %>'>[edit]</a>
                            <%
                            }
                        %>
                    </fieldset>
                    <fieldset>
                        <legend>Description</legend>
                        <div>
                            <%= query.text() %>
                        </div>
                        <%
                        if (data.owner(query))
                            {
                            %>
                            <a href='<%= data.path(query, "edit") %>'>[edit]</a>
                            <%
                            }
                        %>
                    </fieldset>

                    <fieldset>
                        <legend>ADQL query</legend>
                        <div>
                            <%= query.query() %>
                        </div>
                        <%
                        if (data.owner(query))
                            {
                            %>
                            <a href='<%= data.path(query, "edit") %>'>[edit]</a>
                            <%
                            }
                        %>
                    </fieldset>

                    <fieldset>
                        <legend>Tags</legend>
                        <table border='1'>
                            <tr>
                                <th>Owner</th>
                                <th>Name</th>
                            </tr>
                            <%
                            for (QueryTag tag : query.tags().select())
                                {
                                %>
                                <tr>
                                    <td>
                                        <a href='<%= data.path(tag.owner()) %>'><%= tag.owner().name() %></a>
                                    </td>
                                    <td>
                                        <a href='<%= data.path(tag.tag()) %>'><%= tag.tag().name() %></a>
                                    </td>
                                </tr>
                                <%
                                }
                            %>
                        </table>
                        <%
                        if (actor != null)
                            {
                            %>
		                    <form method="post" action="<%= data.path(query, "tags/add") %>">
                          		<input type="text"  name="name"/>
		                        <button type="submit">Add tag</button>
		                    </form>
                            <%
                            }
                        %>
                    </fieldset>
                </fieldset>

                <fieldset>
                    <legend>Results</legend>
                    <fieldset>
                        <legend>Tags</legend>
                        SELECT DISTINCT tag FROM results ....
                        <%
                        if (actor != null)
                            {
                            %>
                            <form method="post" action="<%= data.path(query, "rtags/add") %>">
                          		<input type="text"  name="name"/>
                                <button type="submit">Add tag</button>
                            </form>
                            <%
                            }
                        %>
                    </fieldset>
                    <table border='1'>
                        <tr>
                            <th>Row</th>
                            <th>Object</th>
                        </tr>
                        <%
                        for (Result result : query.results().select())
                            {
                            %>
                            <tr>
                                <td><a href='<%= data.path(result) %>'><%= result.row() %></a></td>
                                <td><%= result.oid() %></td>
                            </tr>
                            <%
                            }
                        %>
                    </table>
                </fieldset>
            </div>
        </div>
        <%@ include file="/jsp/footer.jsp" %>
    </body>
</html>


